package xyz.ifanfan.filter;

import jakarta.servlet.ServletRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

public class RequestProxy implements InvocationHandler {

    private ServletRequest servletRequest;
    private List<String> list;

    public RequestProxy(ServletRequest servletRequest, List<String> list) {
        this.servletRequest = servletRequest;
        this.list = list;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getName().equals("getParameter")) {
            String result = (String) method.invoke(servletRequest, args);
//            result = new String(result.getBytes("iso-8859-1"),"utf-8");
            if (result != null) {
                for (String str : list) {
                    if (result.contains(str)) {
                        result = result.replaceAll(str, "***");
                    }
                }
            }
            return result;
        }
        return method.invoke(servletRequest, args);
    }
}
